/* vim:set noet ts=4: */
/** 
 * scim-python
 * 
 * Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * $Id: $
 */
#include "../../config.h"
#include "../scim-python-config.h"
#include <Python.h>
#include <scim.h>
#include <assert.h>
#include <libintl.h>
#include <gtk/gtk.h>
#include <pygobject.h>
#define _(a) dgettext(GETTEXT_PACKAGE, a)
#define N_(a) (a)
using namespace scim;
using namespace std;

static int _use_count = 0;

static PyObject *
Py_CallFunction (char *module, char *function, PyObject *args)
{
	PyObject *pName = NULL;
	PyObject *pModule = NULL;
	PyObject *pFunc = NULL;
	PyObject *pValue = NULL;

	pName = PyString_FromString (module);
	if (pName == NULL)
		goto _failed_out;

	pModule = PyImport_Import (pName);

	if (pModule == NULL)
		goto _failed_out;
	
	pFunc = PyObject_GetAttrString (pModule, function);
	
	if (pFunc == NULL)
		goto _failed_out;
	
	pValue = PyObject_CallObject (pFunc, args);	

	if (pValue == NULL)
		goto _failed_out;

	goto _success_out;;
	
_failed_out:
	PyErr_Print ();
_success_out:
	Py_XDECREF (pName);
	Py_XDECREF (pFunc);
	Py_XDECREF (pModule);
	
	return pValue;

}

extern "C" void
scim_module_init (void)
{
	bindtextdomain (GETTEXT_PACKAGE, SCIM_PYTHON_LOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	
	if (_use_count > 0) {
		goto out;
	}

	if (Py_IsInitialized ()) {
		goto out;
	}

	static int add_path = 0;
	
	if (add_path == 0) {
		char pythonpath[512] = "PYTHONPATH="SCIM_PYTHON_DATADIR;
		char *old_pythonpath = getenv ("PYTHONPATH");
		
		if (old_pythonpath != NULL) {
			strncat (pythonpath, ":", sizeof (pythonpath));
			strncat (pythonpath, old_pythonpath, sizeof (pythonpath));
		}
		putenv (strdup (pythonpath));
		add_path = 1;
	}

	Py_Initialize ();

out:
	_use_count ++;
}

extern "C" void
scim_module_exit (void)
{
	if (_use_count == 0)
		return;
	_use_count --;
}


extern "C" GtkWidget * 
scim_setup_module_create_ui (void)
{
	static GtkWidget *window = NULL;
	PyObject *pValue = NULL;

	if (window != NULL)
		return window;

	
	pValue = Py_CallFunction (MOD, "mod_create_ui", NULL);	

	if (pValue != Py_None) {
		window = GTK_WIDGET (pygobject_get (pValue));
		gtk_object_ref (GTK_OBJECT (window));
	}

	Py_DECREF (pValue);
	return window;
}

extern "C" String
scim_setup_module_get_category (void)
{
	return String ("IMEngine");
}

extern "C" String
scim_setup_module_get_name (void)
{
	return String (NAME);
}

extern "C" String
scim_setup_module_get_description (void)
{
	return String (DESCRIPTION);
}

extern "C" void
scim_setup_module_load_config (const ConfigPointer &config)
{
	PyObject *pValue = NULL;
	PyObject *pArgs = NULL;

	pArgs = Py_BuildValue ("(O)", PyConfig_New (config));
	pValue = Py_CallFunction (MOD, "mod_load_config", pArgs);
	
	if ( pValue == NULL) {
		PyErr_Clear ();
	}

	Py_XDECREF (pArgs);
	Py_XDECREF (pValue);
}

extern "C" void
scim_setup_module_save_config (const ConfigPointer &config)
{
	PyObject *pValue = NULL;
	PyObject *pArgs = NULL;

	pArgs = Py_BuildValue ("(O)", PyConfig_New (config));
	
	pValue = Py_CallFunction (MOD, "mod_save_config", pArgs);	
	

	Py_XDECREF (pArgs);
	Py_XDECREF (pValue);
}

extern "C" bool
scim_setup_module_query_changed ()
{
	PyObject *pValue = NULL;
	bool retval = false;

	pValue = Py_CallFunction (MOD, "mod_query_changed", NULL);
	
	if (pValue == Py_True) {
		retval = true;
	}

	Py_XDECREF (pValue);

	return retval;
}

